package com.mrd.gtimp.business.action;

import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimp.business.entity.Notification;
import com.mrd.gtimp.business.service.NotificationBiz;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;

/**
 * Created by dengdc on 2017/1/17.
 */
@Controller
@RequestMapping("/notification")
public class NotificationAction {
	@Resource
	private NotificationBiz notificationBiz;

	/**
	 * 打开查询通知功能
	 */
	@RequestMapping("/openQuery")
	@ResponseBody
	public String openQuery() {
		notificationBiz.openQuery();
		return "手动打开通知查询";
	}

	/**
	 * 关闭查询通知功能
	 */
	@RequestMapping("/closeQuery")
	@ResponseBody
	public String closeQuery() {
		notificationBiz.closeQuery();
		return "手动关闭通知查询";
	}

	/**
	 * 获取通知
	 */
	@RequestMapping("/getNotifications")
	@ResponseBody
	public Result getNotifications(String readIds) {
		String[] readIdArray = readIds.split(",");
		List<Notification> notifications = notificationBiz.findShouldReadNotification(readIdArray);
		Result result;
		if(CollectionUtils.isEmpty(notifications)) {
			result = Result.newFailInstance("暂时没有任何通知");
		} else {
			result = Result.newSuccessInstance("总计：" + notifications.size() + "条通知", notifications);
		}
		return result;
	}
}
